package com.bw.service.impl;

import com.alibaba.fastjson.JSON;
import com.bw.entity.Esvo;
import com.bw.entity.Users;
import com.bw.entity.Vo;
import com.bw.mapper.UsersMapper;
import com.bw.service.IUsersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.netty.util.Timeout;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundGeoOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import springfox.documentation.spring.web.json.Json;

import java.io.IOException;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 王墨
 * @since 2021-11-05
 */
@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements IUsersService {

    @Autowired
    private UsersMapper usersMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private RestHighLevelClient client;


    @Override
    public int udel(int userId) {
        return usersMapper.udel(userId);
    }

    @Override
    public List<Users> ulist(Vo vo) {
        return usersMapper.ulist(vo);
    }

    @Override
    public int uadd(Users users) {
        usersMapper.uadd(users);
        return 0;
    }

    @Override
    public int uset(Users users) {
        return usersMapper.uset(users);
    }

    @Override
    public Users hx(int userId) {
        return usersMapper.hx(userId);
    }

    @Override
    public List<Users> relist() {
        List<Users> relist = usersMapper.relist();
        String s = JSON.toJSONString(relist);
        redisTemplate.opsForValue().set("aa",s);
        return null;
    }

    @Override
    public String uplist() {
        BulkRequest request = new BulkRequest();
        List<Users> users = baseMapper.selectList(null);
        for (Users user : users) {
            IndexRequest posts = new IndexRequest("posts");
            posts.id(user.getUserId().toString());
            String s = JSON.toJSONString(user);
            posts.source(s, XContentType.JSON);
            request.add(posts);
        }
        try {
            client.bulk(request, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Users> eslist(Esvo esvo) {
//        SearchRequest eslist = new SearchRequest("eslist");
//        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
//        if (esvo != null) {
//            //查地区
//            if (esvo.getUserName() != null) {
//                boolQueryBuilder.filter(QueryBuilders.matchQuery("userName", esvo.getUserName()));
//            }
//        }
        return null;
    }


}
